package g11.action;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import g11.actionutil.*;
import g11.dao.*;
import g11.model.*;

public class InitUserMainAction extends ActionSupport implements SessionAware,RequestAware {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7749185040942268233L;
	
	private Map<String,Object> session;
	private Map<String,Object> request;
	
	private List<Appointment> patientAppointment;
	
	private List<Appointment> doctorTodayAppointment;
	private List<Appointment> staffTodayAppointment;
	//retrievedDoctorList is used for legal main page
	private List<Employee> retrievedDoctorList;
	
	private final Logger log = Logger.getLogger(this.getClass());
	
	@Override
	public void setRequest(Map request) {
		this.request = request;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}
	
	public List<Appointment> getPatientAppointment() {
		return patientAppointment;
	}

	public void setPatientAppointment(ArrayList<Appointment> patientAppointment) {
		this.patientAppointment = patientAppointment;
	}

	public List<Appointment> getDoctorTodayAppointment() {
		return doctorTodayAppointment;
	}

	public void setDoctorTodayAppointment(List<Appointment> doctorTodayAppointment) {
		this.doctorTodayAppointment = doctorTodayAppointment;
	}

	public List<Appointment> getStaffTodayAppointment() {
		return staffTodayAppointment;
	}

	public void setStaffTodayAppointment(List<Appointment> staffTodayAppointment) {
		this.staffTodayAppointment = staffTodayAppointment;
	}

	public List<Employee> getRetrievedDoctorList() {
		return retrievedDoctorList;
	}

	public void setRetrievedDoctorList(List<Employee> retrievedDoctorList) {
		this.retrievedDoctorList = retrievedDoctorList;
	}
	
	public void createAppointments(){
		Employee employee = new Employee();
		employee.setUsername("doctor2");
		employee.setPassword("0000");
		Byte type = 1;
		employee.setType(type);
		employee.setName("Marc Lemercier");
		employee.setDepartment("Surgery");
		employee.setTitle("Attendance Doctor");
		EmployeeDAO employeeDao = new EmployeeDAOImpl();
		try {
			employeeDao.createEmployee(employee);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Employee retrievedEmployee = new Employee();
		try {
			retrievedEmployee = employeeDao.getEmployeeByUsername("mama");
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Patient retrievedPatient = (Patient)session.get("currentUser");
		
		for(int i = 0;i<5;i++){
			// create a appointment for this doctor and this patient
			Appointment appointment = new Appointment();
			appointment.setDoctor(retrievedEmployee);
			appointment.setPatient(retrievedPatient);
			//set date and time for this appointment: set date to Mar 1st,2012
			Date tempDate=new Date(112,2,1+i);
			appointment.setAppointmentDate(tempDate);
			appointment.setStartTime("10:3"+2*i);
			appointment.setEndTime("11:4"+2*i);
			appointment.setLocation("MC 200"+i);
			//set status to pending
			byte status=0;
			appointment.setStatus(status);
			//test to persist the appointment,createAppointment(appointment)
			AppointmentDAO appointmentDao = new AppointmentDAOImpl();
			try {
				appointmentDao.createAppointment(appointment);
			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public void createVisitationContent(){
		EmployeeDAO employeeDao = new EmployeeDAOImpl();
		Employee retrievedEmployee = new Employee();
		try {
			retrievedEmployee = employeeDao.getEmployeeByUsername("mama");
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Patient retrievedPatient = (Patient)session.get("currentUser");
		
		for(int i = 0;i<5;i++){
			// create a appointment for this doctor and this patient
			Appointment appointment = new Appointment();
			appointment.setDoctor(retrievedEmployee);
			appointment.setPatient(retrievedPatient);
			//set date and time for this appointment: set date to Mar 1st,2012
			Date tempDate=new Date(112,2,1+i);
			appointment.setAppointmentDate(tempDate);
			appointment.setStartTime("10:3"+2*i);
			appointment.setEndTime("11:4"+2*i);
			appointment.setLocation("MC 200"+i);
			//set status to pending
			byte status=0;
			appointment.setStatus(status);
			//test to persist the appointment,createAppointment(appointment)
			AppointmentDAO appointmentDao = new AppointmentDAOImpl();
			Integer appId = 0;
			try {
				appId = appointmentDao.createAppointment(appointment);
			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Appointment retrievedAppointment = new Appointment();
			try {
				retrievedAppointment = appointmentDao.getAppointmentById(appId);
			} catch (DAOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			VisitationContent visitationContent = new VisitationContent();
			visitationContent.setComment("This is the comment");
			visitationContent.setDiagnosis("Good situation+"+i);
			visitationContent.setTreatment("Surgery+"+i);
			VisitationContentUtilImpl vtImpl = new VisitationContentUtilImpl();
			
			try {
				vtImpl.createVisitationContentFromAppointment(retrievedAppointment, visitationContent);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	// TODO
	public String initPatientMain() {
		AppointmentDAOImpl appDao = new AppointmentDAOImpl();
		
		Patient p = (Patient)session.get("currentUser");
		
		PatientUtilImpl pui = new PatientUtilImpl();
		Employee defaultDoctor = new Employee();
		defaultDoctor = pui.getDefaultDoctorByPatientId(p.getId());
		
		String doctorName = "No default doctor";
		if(defaultDoctor != null){
			doctorName = defaultDoctor.getName();
		}
		session.put("defaultDoctorName", doctorName);
		
		try {
			this.patientAppointment = appDao.getPendingAppointmentByPatient(p);
		} catch (DAOException e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	// TODO
	public String initStaffMain() {
		AppointmentUtilImpl appImpl = new AppointmentUtilImpl();
		Employee staff = (Employee)session.get("currentUser");
		List<Appointment> todayAppointment = new ArrayList<Appointment>();
		try {
			todayAppointment = appImpl.getTodayAppointmentByStaff(staff);
		} catch (DAOException e) {
			e.printStackTrace();
			return ERROR;
		}
		this.staffTodayAppointment = todayAppointment;
		
		return SUCCESS;
	}

	// TODO
	public String initDoctorMain() {
		AppointmentDAOImpl appDao = new AppointmentDAOImpl();
		Employee doctor = (Employee)session.get("currentUser");
		List<Appointment> todayAppointment = new ArrayList<Appointment>();
		try {
//			todayAppointment = appDao.getPendingAppointmentByDoctor(doctor);
			todayAppointment = appDao.getTodayAppointmentByDoctor(doctor);
		} catch (DAOException e) {
			e.printStackTrace();
			return ERROR;
		}
		this.doctorTodayAppointment = todayAppointment;
		
		return SUCCESS;
	}

	// TODO
	public String initFinanceMain() {
		return SUCCESS;
	}

	// TODO
	public String initLegalMain() {
		
		EmployeeDAOImpl eDao = new EmployeeDAOImpl();
		try {
			this.retrievedDoctorList = eDao.getAllEmployee("doctor");
			
		} catch (DAOException e) {
			log.error(
					"Error: EmployeeAction: initEmployeeListPage():getAllEmployee",
					e);
			return ERROR;
		}
		return SUCCESS;
	}

	// TODO
	public String initAdminMain() {
		
		
		return SUCCESS;
	}

	
}
